package com.yachang.qym.usbhost.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.utils.DeveloperData;
import com.yachang.qym.usbhost.utils.SendData;

public class TestActivity extends AppCompatActivity {

    private EditText setEle;
    private TextView lastEle;
    private TextView nowEle;
    private TextView allEle;
    private TextView tvDebugData;
    private RelativeLayout sendEle;
    private RelativeLayout enterDeveloper;
    private Toolbar toolbar;
    private boolean threadCt = true;
    private Handler mHandler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what) {
                case 0: {
                    bingData();
                }break;
                default:break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        SendData.data = SendData.enterTestModel();
        SendData.needSend = true;
        initView();
        new ChangeDataThread().start();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private void initView(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("开发者模式");
        setEle = (EditText) findViewById(R.id.et_set_ele);
        lastEle = (TextView) findViewById(R.id.tv_last_ele);
        nowEle = (TextView) findViewById(R.id.tv_now_ele);
        allEle = (TextView) findViewById(R.id.tv_all_ele);
        tvDebugData = (TextView) findViewById(R.id.tv_debug_data);
        sendEle = (RelativeLayout) findViewById(R.id.layout_send_ele);
        enterDeveloper = (RelativeLayout) findViewById(R.id.layout_enter_test_model);
        sendEle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(setEle.getText().toString().trim().equals("")){
                    Toast.makeText(TestActivity.this, "请输入电位设置！", Toast.LENGTH_SHORT).show();
                    return;
                }
                int count = Integer.parseInt(setEle.getText().toString().trim());
                if(count < 0 || count > 256){
                    Toast.makeText(TestActivity.this, "电位设置出错，请输入0~255之间的值！", Toast.LENGTH_SHORT).show();
                    return;
                }
                SendData.data = SendData.sendChangeEle(Integer.parseInt(setEle.getText().toString().trim()));
                SendData.needSend = true;
            }
        });
        enterDeveloper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendData.data = SendData.sendEnterDeveloper();
                SendData.needSend = true;
            }
        });
        bingData();
    }

    private void bingData(){
        nowEle.setText(DeveloperData.nowEle);
        allEle.setText(DeveloperData.allEle);
        lastEle.setText(DeveloperData.lastEle + "");
        tvDebugData.setText(DeveloperData.debugData);
    }

    private class ChangeDataThread extends Thread{
        public void run(){
            while (threadCt) {
                if (DeveloperData.needChange) {
                    Message message = new Message();
                    message.what = 0;
                    mHandler.sendMessage(message);
                    DeveloperData.needChange = false;
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        threadCt = false;
        SendData.data = SendData.exitTestModel();
        SendData.needSend = true;
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home :
                TestActivity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
